﻿using GUI.RLAServiceReference;
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel.Channels;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace GUI.Tabpages
{
    /// <summary>
    /// Interaction logic for PageSearch.xaml
    /// </summary>
    public partial class PageSearch : Page
    {
        private User loggedInUser;
        private RLAServiceReference.IRealLifeService client = new RLAServiceReference.RealLifeServiceClient();

        public PageSearch(User user)
        {
            InitializeComponent();
            loggedInUser = user;
        }

        private void btnSearch_Click(object sender, RoutedEventArgs e)
        {
            //var results = new List<User>();
            string searchTerms = txtSearch.Text;

            //string[]                                             minus (-)toList Kommentar af jan
            string[] keywords = searchTerms.Split(new char[0]);
            // results.Add(loggedInUser);

            var results = client.SearchUserList(keywords.ToList());
            LvSearch.DataContext = results;
        }

        private void btnRequestFriendship_Click(object sender, RoutedEventArgs e)
        {
            //We know our sender is a button. This button should contain a user in its datacontext.
            if (((Button)sender).DataContext.GetType() == typeof(User))
            {
                User btnUser = (User)((Button)sender).DataContext;

                if (loggedInUser.UserId != btnUser.UserId)
                {
                    int friendshipRequest = client.RequestFriendship(loggedInUser, btnUser);
                }
            }
        }

        private void txtSearch_GotFocus(object sender, RoutedEventArgs e)
        {
            if (txtSearch.Text == "Søg . . .")
            {
                txtSearch.Text = "";
            }
        }

        private void txtSearch_LostFocus(object sender, RoutedEventArgs e)
        {
            if (txtSearch.Text == "")
            {
                txtSearch.Text = "Søg . . .";
            }
        }
    }
}